package com.xiaoshuidi.cloud.module.rooms.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.mybatis.core.dataobject.BaseDO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.equipmentStocktaking.vo.EquipmentStocktakingPageReqVO;
import com.xiaoshuidi.cloud.module.rooms.mapper.EquipmentStocktakingMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.EquipmentStocktaking;
import com.xiaoshuidi.cloud.module.rooms.repository.irepository.IEquipmentStocktakingRepository;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

/**
 * @Description
 * @Param
 * @Return
 * @Date 2023/5/21 17:20
 * @Author Link
 **/
@Repository
@RequiredArgsConstructor
public class EquipmentStocktakingRepository extends ServiceImpl<EquipmentStocktakingMapper, EquipmentStocktaking> implements IEquipmentStocktakingRepository {

    private final EquipmentStocktakingMapper mapper;

    @Override
    public PageResult<EquipmentStocktaking> getEquipmentStocktakingPage(EquipmentStocktakingPageReqVO reqVO) {
        //分类名称 分类编码 复合查询
        // 条件1：  盘点人id为登录人 or 创建者id为登录人
        LambdaQueryWrapper<EquipmentStocktaking> wrapper = new LambdaQueryWrapper<EquipmentStocktaking>()
                .ge(ObjectUtils.isNotEmpty(reqVO.getPdBeginTime()), EquipmentStocktaking::getPdTime, reqVO.getPdBeginTime())
                .le(ObjectUtils.isNotEmpty(reqVO.getPdEndTime()), EquipmentStocktaking::getPdTime, reqVO.getPdEndTime())
                .eq(StringUtils.isNotEmpty(reqVO.getStatus()), EquipmentStocktaking::getStatus, reqVO.getStatus())
                .and(StringUtils.isNotEmpty(reqVO.getKeyWord()), w -> w.like(StringUtils.isNoneEmpty(reqVO.getKeyWord()), EquipmentStocktaking::getInvArea, "%" + reqVO.getKeyWord() + "%")
                        .or()
                        .like(StringUtils.isNoneEmpty(reqVO.getKeyWord()), EquipmentStocktaking::getNo, "%" + reqVO.getKeyWord() + "%")
                        .or()
                        .like(StringUtils.isNoneEmpty(reqVO.getKeyWord()), EquipmentStocktaking::getCheckedName, "%" + reqVO.getKeyWord() + "%"))
                .orderByDesc(EquipmentStocktaking::getUpdateTime);
        return mapper.selectPage(reqVO, wrapper);
    }
}
